package com.itheima.algorithm.binarysearch;

public class E01Leetcode704 {
    public static int search1(int [] a,int target){
        int i=0,j=a.length-1;
        while(i<=j){
            int m=(i+j)>>>1;
            if(target<a[m]){
                j=m-1;
            }else if(a[m]<target){
                i=m+1;
            }else{
                return m;
            }
        }
        return -1;
    }

    public static int search2(int [] a,int target){
        int i=0,j=a.length;
        while(i<j){
            int m=(i+j)>>>1;
            if(a[m]<target){
                j=m;
            }else if(target<a[m]){
                i=m+1;
            }else{
                return m;
            }
        }
        return -1;
    }

    public static int search3(int [] a,int target){
        int i=0,j=a.length;
        while(1+i<j){
            int m=(i+j)>>>1;
            if(target<a[m]){
                j=m;        //左移动
            }else{
                i=m;
            }
        }
        return (a[i]==target)?i:-1;
    }
}
